////////////////////////////////////////////////////////////////////////////
//
//  AirCapture - Site Capture library
//  Copyright 2008 CRE8SYSTEM All Rights Reserved.
//
//  NOTICE: CRE8SYSTEM. permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////
package jp.cre8system.framework.aircapture.sitecapture.components
{
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Matrix;
	import flash.html.HTMLLoader;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	import jp.cre8system.framework.aircapture.sitecapture.events.SiteCaptureEvent;
	
	import mx.core.Application;
	import mx.core.UIComponent;

	[Event(name="captureComplete", type="jp.cre8system.framework.aircapture.sitecapture.events.SiteCaptureEvent")]

	public class SiteCapture extends UIComponent
	{
		
		public var url:String;
		public var handler:Function;
		public var captureWidth:int;
		public var captureHeight:int;
		public var contentWidth:int;
		public var contentHeight:int;
		public var bitmapData:BitmapData;
		
		public var maxContentWidth:int = 800;
		
		private var _html:HTMLLoader;
		private var _appAdd:Boolean = false;
		
		private const WAIT_TIMER_MSEC:int = 100;
		
		public function SiteCapture()
		{
			super();
		}
		
	    override public function initialize():void
	    {
            _html = new HTMLLoader();
        	_html.visible = true;
            _html.addEventListener(Event.COMPLETE, _htmlLoadComplete);

            addChild(_html);
			visible = false;

	    	super.initialize();
	    }
		
		public function capture(url:String, width:int = 0, height:int = 0):void
		{
			this.url = url;
			this.handler = handler;
			this.captureWidth = width;
			this.captureHeight = height;

			if (!_html)
			{
				Application.application.addChild(this);
				_appAdd = true;
			}
			
			trace("Site Capture html load start " + this.url);
			_html.load(new URLRequest(url));
			
		} 

		private function _htmlLoadComplete(event:Event):void
		{
			trace("Site Capture html load complete " + this.url);
            _html.width = _html.contentWidth;
            if (_html.width > maxContentWidth)
            {
            	_html.width = maxContentWidth;
            }
            if (_html.contentHeight > 2880)
            {
            	_html.height = 2880;
            } else {
            	_html.height = _html.contentHeight;
            }
            contentWidth = _html.width;
            contentHeight = _html.height;
            if (contentWidth == 0)
            {
            	contentWidth = 800;
            }
            if (contentHeight == 0)
            {
            	contentHeight = 600;
            }
            callLater(_callLaterHandler);
		}

        private function _callLaterHandler():void
        {
            var timer:Timer = new Timer(WAIT_TIMER_MSEC, 1);
            timer.addEventListener(TimerEvent.TIMER_COMPLETE, _createBitmap);
            timer.start();
        }
		
        private function _createBitmap(event:Event = null):void
        {
        	if (contentWidth == 0) contentWidth = 1;
        	if (contentHeight == 0) contentHeight = 1;

            var p:Number = captureWidth / contentWidth;
            
            var fullBitmap:BitmapData = new BitmapData(contentWidth , contentHeight);
            fullBitmap.draw(_html, new Matrix(1, 0, 0, 1));
            
            bitmapData = new BitmapData(captureWidth , captureHeight);
            bitmapData.draw(fullBitmap, new Matrix(p, 0, 0, p), null, null, null, true);
            
            if (_appAdd)
            {
            	Application.application.removeChild(this);
            }
            
			trace("Site Capture bitmap created.");
            dispatchEvent(new SiteCaptureEvent(SiteCaptureEvent.CAPTURE_COMPLETE));
        }
		
	}
}